package com.lalosoft.misgastos.email;

import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.lalosoft.misgastos.config.AESEncrypt;
import com.lalosoft.misgastos.config.Constants;
import com.lalosoft.misgastos.config.EmailBody;
import com.lalosoft.misgastos.config.GastosManager;
import com.lalosoft.misgastos.domain.Account;
import com.lalosoft.misgastos.domain.Item;
import com.lalosoft.misgastos.service.GastosTarjetaService;
import com.lalosoft.misgastos.service.impl.GastosTarjetaServiceImpl;

public class EmailSenderManager {
	
	private GmailSender gmailSender;
	private static EmailSenderManager instance;
	private Context context;
	
	public static EmailSenderManager getInstance(Context context) throws Exception{
		if(instance == null){
			instance = new EmailSenderManager(context);
		}
		return instance;
	}
	
	private EmailSenderManager(Context context) throws Exception{
		this.context = context;
		gmailSender = new GmailSender(Constants.GMAIL_USER, obtaingPass());
	}
	
	public void sendEmail() throws Exception{
		EmailBody body = fillBody();
    	Account account = GastosManager.getInstance(context).getCurrentAccount();
		DateFormat df = DateFormat.getDateInstance();
		String month = df.format(new Date(System.currentTimeMillis()));
		gmailSender.sendMail("Gastos Tarjeta - " + month, body.toString(), Constants.GMAIL_USER, account.getEmail());
		Log.d("EmailSender", "Se envio correctamente el mail");
	}
	
	private String obtaingPass() throws Exception {
		GastosTarjetaService service = new GastosTarjetaServiceImpl();
		String json = service.obtaingData(Constants.TOKEN);
		String pass = "";
		if(!json.equals("")){
			JSONObject jsonObject = new JSONObject(json);
			AESEncrypt aes = new AESEncrypt();
			aes.setKey(jsonObject.getString("key"));
			pass = aes.decrypt(jsonObject.getString("password"));
		}
		return pass;
	}

	private EmailBody fillBody() throws SQLException{
		EmailBody body = new EmailBody();
		Account account = GastosManager.getInstance(context).getCurrentAccount();
		body.add("Hola " + account.getUsername() + ", este es un resumen de gastos para este mes.");
		body.add("Se te seguira enviando a: " + account.getEmail());
		body.addLine();
		body.addLine();
		double total = 0;
    	List<Item> list = GastosManager.getInstance(context).getAllItems();
    	for(Item temp : list){
    		body.addLine();
    		body.add("+ Item +");
    		body.addLine();
    		body.add("-Detalle:");
    		body.add(temp.getDetalle());
    		body.add("-Fecha de compra: ");
    		body.add(temp.getFechaCompra());
    		body.add("-Valor Cuota:");
    		body.add(String.valueOf(temp.getValorCuota()));
    		body.add("-Cuotas restantes:");
    		body.add(String.valueOf(temp.getCuotasRestantes()));
    		body.addLine();
    		body.add("-----------------------------------------");
    		total = total + temp.getValorCuota();
    	}
    	body.addLine();
    	body.add("Total a pagar: " + total);
    	return body;
	}

}
